package online.lmds.sdmanage.main.upload_image.service;


import online.lmds.sdmanage.common.Constants;
import online.lmds.sdmanage.common.Status;
import online.lmds.sdmanage.common.entity.Result;
import online.lmds.sdmanage.main.base.UploadImage;
import online.lmds.sdmanage.main.upload_image.dao.UploadImageDao;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

@Service("UploadImageService")
public class UploadImageServiceImpl implements UploadImageService {
    
    @Resource(name = "UploadImageDao")
    private UploadImageDao uploadImageDao;

    @Override
    public Result save(UploadImage uploadImage) {
        Result result = new Result();
        if (uploadImage == null || StringUtils.isEmpty(uploadImage.getImgUrl())) {
            return result.setErrorCode().setMessage("无效的图片URL").setStatus(Status.BASE_FILE_PATH_ERR);
        }
        if (uploadImage.getUserId() == null) {
            return result.setErrorCode().setMessage("请先登录").setStatus(Status.BASE_NEED_LOGIN);
        }
        uploadImage.setId(null);
        uploadImageDao.save(uploadImage);
        if (uploadImage.getId() == null) {
            return result.setErrorCode().setMessage("保存上传图片记录失败");
        } else {
            uploadImage.setAdminId(null);
            uploadImage.setUserId(null);
            uploadImage.setImgUrl(Constants.BASE_IMG_URL + uploadImage.getImgUrl());
            return result.setSuccessCode().setData(uploadImage).setMessage("success");
        }
    }

    @Override
    public int count(UploadImage uploadImage) {
        return uploadImageDao.count(uploadImage);
    }

}
